//*********************************************************
//
//    Copyright (c) Microsoft. All rights reserved.
//    This code is licensed under the Apache License, Version 2.0.
//    THIS CODE IS PROVIDED *AS IS* WITHOUT WARRANTY OF
//    ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY
//    IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR
//    PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT.
//
//*********************************************************

namespace Microsoft.Research.ScientificWorkflow.ManagementStudioControls.RegistryManagerModule
{
    using System.ComponentModel;
    using System.Windows;
    using ManagementStudioServices.Interfaces;
    using Microsoft.Research.ScientificWorkflow.ManagementStudioControls.Common;
    using Microsoft.Research.ScientificWorkflow.ManagementStudioModels;
    using Microsoft.Research.ScientificWorkflow.TridentUtilities;
    using Microsoft.Research.ScientificWorkflow.UIDesigner.WpfComposite;
    using Microsoft.Practices.Unity;
    using Microsoft.Research.ScientificWorkflow.TridentUtilities.Properties;

    /// <summary>
    ///  Assembly Catalog CenterPane Presenter
    /// </summary>
    public class AssemblyCatalogueCenterPanePresenter : IRegistryManagerCenterPanePresenter, INotifyPropertyChanged
    {
        /// <summary>
        /// Holds the IUnityContainer 
        /// </summary>
        private IUnityContainer container;

        /// <summary>
        /// Holds the Instance of IRegistryManagerService
        /// </summary>
        private IRegistryManagerService registryManagerService;

        /// <summary>
        /// Holds the AssemblyPackage ViewModel
        /// </summary>
        private AssemblyPackageViewModel selectedViewModel;

        /// <summary>
        /// Get and Set label for the Refered list.
        /// </summary>
        public string ListLabel { get; set; }

        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="container"></param>
        /// <param name="contentView"></param>
        /// <param name="service"></param>
        public AssemblyCatalogueCenterPanePresenter(IUnityContainer container,
            AssemblyCatalogueCenterPaneView contentView,
            IRegistryManagerService service)
        {
            this.container = container;
            this.ContentView = contentView;
            contentView.DataContext = this;
            this.registryManagerService = service;
            this.DeleteAssemblyCommand = new DelegateCommand<object>(p => this.DeleteAssembly());
        }

        /// <summary>
        /// Delete Command
        /// </summary>
        public DelegateCommand<object> DeleteAssemblyCommand { get; set; }

        #region IRegistryManagerCenterPanePresenter Members

        /// <summary>
        /// Gets or sets the content view.
        /// </summary>
        public UIElement ContentView
        {
            get;
            set;
        }

        /// <summary>
        /// Gets or sets the refresh command.
        /// </summary>
        /// <value>The refresh command.</value>
        public DelegateCommand<BaseRegMgrModel> RefreshCommand { get; set; }

        /// <summary>
        /// Gets or sets the update command.
        /// </summary>
        /// <value>The delete command.</value>
        public DelegateCommand<BaseRegMgrModel> UpdateCommand { get; set; }

        /// <summary>
        /// Gets or sets the delete command.
        /// </summary>
        /// <value>The delete command.</value>
        public DelegateCommand<BaseRegMgrModel> DeleteCommand { get; set; }

        /// <summary>
        /// Gets or sets the select root command.
        /// </summary>
        /// <value>The select root command.</value>
        public DelegateCommand<object> SelectRootCommand { get; set; }

        /// <summary>
        /// Gets or sets the selected category view model.
        /// </summary>
        /// <value>The selected category view model.</value>
        public BaseRegMgrModel SelectedModel
        {
            get
            {
                return this.selectedViewModel;
            }
            set
            {
                this.selectedViewModel = value as AssemblyPackageViewModel;
                this.RaisePropertyChanged("SelectedModel");
            }
        }

        /// <summary>
        /// Saves the changes.
        /// </summary>
        /// <returns>True if save is success.</returns>
        public SaveStateHandler SaveChanges()
        {
            return new SaveStateHandler(SaveStatus.Valid);
        }

        #endregion

        /// <summary>
        /// Delete the selected Assembly
        /// </summary>
        private void DeleteAssembly()
        {
            try
            {
                if (TridentMessageBox.ShowTridentConfirmationYesNoMessageBox(ManagementStudio.AssemblyDeleteConfirmation) == MessageBoxResult.No)
                {
                    return;
                }

                this.registryManagerService.DeletePackage(selectedViewModel.Id);
                this.DeleteCommand.Execute(this.selectedViewModel);
                this.container.Resolve<IStatusMessageControl>().SetText(ManagementStudio.AssemblyDeleteStatusMessage);
            }
            catch (TridentCustomException ex)
            {
                TridentMessageBox.ShowTridentErrorMessageBox(ex.Message);
            }
        }

        #region INotifyPropertyChanged Members

        public event PropertyChangedEventHandler PropertyChanged;

        /// <summary>
        /// RaiseProperty Changed event handler
        /// </summary>
        /// <param name="name">name</param>
        protected void RaisePropertyChanged(string name)
        {
            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs(name));
        }

        #endregion
    }
}
